package com.itxiuyixiu.volunteer.controller;

import com.itxiuyixiu.entity.model.Admin;
import com.itxiuyixiu.entity.model.User;
import com.itxiuyixiu.tools.util.returns.ReturnVo;
import com.itxiuyixiu.volunteer.dto.AdminModifyDetailsMessageParam;
import com.itxiuyixiu.volunteer.dto.UserModifyDetailMessageParam;
import com.itxiuyixiu.volunteer.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @author 黄磊
 * @date 2020/5/2
 */
@Api(tags = "网站运营者API")
@Validated
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "查找所有的管理员", response = Admin.class, responseContainer = "List")
    @GetMapping("/admins")
    public ReturnVo getAllAdmin() {
        return ReturnVo.getSuccessReturn(adminService.findAllAdmin());
    }

    @ApiOperation(value = "根据管理员id查找管理员信息", response = Object.class)
    @GetMapping("/admin/{adminId}")
    public ReturnVo getAdminById(@PathVariable("adminId") Integer adminId) {
        Admin admin = adminService.findAdminById(adminId);
        if (admin == null) {
            return ReturnVo.getFailureReturn("没有找打相应的管理员信息");
        }
        return ReturnVo.getSuccessReturn(admin);
    }

    @ApiOperation(value = "修改管理员信息", response = String.class)
    @PutMapping("/admin/message/{adminId}")
    public ReturnVo modifyAdminMessageById(@PathVariable("adminId") Integer adminId, @Valid AdminModifyDetailsMessageParam admin, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ReturnVo.getFailureReturn(bindingResult);
        }
        switch (adminService.modifyAdminMessage(adminId, admin)) {
            case 0:
                return ReturnVo.getSuccessReturn("成功修改管理员信息");
            case 1:
                return ReturnVo.getFailureReturn("没有找到管理员信息");
            default:
                return ReturnVo.getFailureReturn("服务器发生未知错误");
        }
    }

    @ApiOperation(value = "修改管理员密码", response = String.class)
    @PutMapping("/admin/password/{adminId}")
    public ReturnVo modifyAdminPasswordById(@PathVariable("adminId") Integer adminId, @NotEmpty(message = "密码不能为空") String password) {
        switch (adminService.modifyAdminPassword(adminId, password)) {
            case 0:
                return ReturnVo.getSuccessReturn("成功修改管理员密码");
            case 1:
                return ReturnVo.getFailureReturn("没有找到管理员信息");
            default:
                return ReturnVo.getFailureReturn("服务器发生未知错误");
        }
    }


    @ApiOperation(value = "查找所有的用户信息", response = User.class, responseContainer = "List")
    @GetMapping("/users")
    public ReturnVo findAllUser() {
        return ReturnVo.getSuccessReturn(adminService.findAllUser());
    }

    @ApiOperation(value = "根据用户id查找用户信息", response = Object.class)
    @GetMapping("/user/{userId}")
    public ReturnVo findUserByUserId(@PathVariable("userId") Integer userId) {
        User user = adminService.findUserById(userId);
        if (user == null) {
            return ReturnVo.getFailureReturn("用户不存在");
        }
        return ReturnVo.getSuccessReturn(user);
    }

    @ApiOperation(value = "修改用户信息", response = String.class)
    @PutMapping("/user/message/{userId}")
    public ReturnVo modifyUserMessage(@PathVariable("userId") Integer userId, @Valid UserModifyDetailMessageParam dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ReturnVo.getFailureReturn(bindingResult);
        }
        switch (adminService.modifyUserMessage(userId, dto)) {
            case 0:
                return ReturnVo.getSuccessReturn("成功修改管理员信息");
            case 1:
                return ReturnVo.getFailureReturn("没有找到管理员信息");
            default:
                return ReturnVo.getFailureReturn("服务器发生未知错误");
        }
    }

    @ApiOperation(value = "修改用户密码", response = String.class)
    @PutMapping("/user/password/{userId}")
    public ReturnVo modifyUserPassword(@PathVariable("userId") Integer userId, @NotEmpty String password) {
        switch (adminService.modifyUserPassword(userId, password)) {
            case 0:
                return ReturnVo.getSuccessReturn("成功修改管理员密码");
            case 1:
                return ReturnVo.getFailureReturn("没有找到管理员信息");
            default:
                return ReturnVo.getFailureReturn("服务器发生未知错误");
        }
    }

    @ApiOperation(value = "查看所有未审核的管理员", response = String.class)
    @GetMapping("/register/checks")
    public ReturnVo getAllRegisterChecks() {
        return ReturnVo.getSuccessReturn(adminService.getAllRegisterChecks());
    }

    @ApiOperation(value = "管理员注册审核", response = String.class)
    @PutMapping("/register/check/{adminId}")
    public ReturnVo registerCheck(@PathVariable("adminId") Integer adminId, @NotNull(message = "passed不能为空") Boolean passed) {
        switch (adminService.registerCheck(adminId, passed)) {
            case 0:
                return ReturnVo.getSuccessReturn("修改审核为通过成功");
            case 1:
                return ReturnVo.getSuccessReturn("修改审核为不通过成功");
            case 2:
                return ReturnVo.getFailureReturn("管理员信息未查找到");
            default:
                return ReturnVo.getFailureReturn("服务器发生未知错误");
        }
    }

    @ApiOperation(value = "管理员注册审核", response = String.class)
    @GetMapping("/admin/register/unpassed")
    public ReturnVo findAllUnpassedAdmin() {
        return ReturnVo.getSuccessReturn(adminService.findAllUnpassedAdmin());
    }
}
